#coding=utf-8
import wx
import imagesplash
import gdata.calendar.service

idLoginButton = wx.NewId()
idTimer = wx.NewId()
idConnectImmediately = wx.NewId()
idStopConnecting = wx.NewId()

class FormLogin(wx.Dialog):
    formMain = None
    bEncodedPasswd = False
    sPasswd = ''
    
    def __init__(self, title):
        wx.Dialog.__init__(self, None, -1, title, 
            wx.DefaultPosition, (406, 240),
            0
            | wx.MINIMIZE_BOX
            | wx.SYSTEM_MENU
            | wx.CAPTION
            | wx.CLOSE_BOX
            )

        self.GoogleCalendar = gdata.calendar.service.CalendarService()

        self.sizer = wx.FlexGridSizer(5, 1, 0, 0)
        self.sizer.AddGrowableRow(2)
        self.sizer.AddGrowableRow(3)
        self.sizer.AddGrowableCol(0)
        
        self.sizerMain = wx.BoxSizer(wx.HORIZONTAL)
        self.sizerProcess = wx.BoxSizer(wx.VERTICAL)
        self.sizerOpt = wx.BoxSizer(wx.HORIZONTAL)
        
        self.editAccount = wx.TextCtrl(self, -1, 
            wx.GetTranslation('Your Google Account'),
            wx.DefaultPosition, (180, 20))
        self.editPasswd = wx.TextCtrl(self, -1, wx.EmptyString, 
            wx.DefaultPosition, (180, 20), wx.TE_PASSWORD)

        self.cbRemAccount = wx.CheckBox(self, -1, wx.GetTranslation('Remember account'))
        self.cbRemPasswd = wx.CheckBox(self, -1, wx.GetTranslation('Remember password'))
        self.cbAutoLogin = wx.CheckBox(self, -1, wx.GetTranslation('Auto login'))

        self.staticCountdown = wx.StaticText(self)
    
        sizerInput = wx.FlexGridSizer(2, 2, 0, 0)
        sizerInput.Add(wx.StaticText(self, -1, 
            wx.GetTranslation('Account')), 0, wx.ALL, 3)
        sizerInput.Add(self.editAccount, 0, wx.ALL, 3)
        sizerInput.Add(wx.StaticText(self, -1, 
            wx.GetTranslation('Password')), 0, wx.ALL, 3)
        sizerInput.Add(self.editPasswd, 0, wx.ALL, 3)
        
        self.sizerMain.Add(sizerInput, 0, wx.ALL, 2)
        self.sizerMain.Add(wx.Button(self, idLoginButton, wx.GetTranslation('Login'),
            wx.DefaultPosition, (50, 50)), 0, wx.ALL, 3);
    
        sizerCountdown = wx.BoxSizer(wx.HORIZONTAL)
        sizerCountdown.Add(wx.StaticText(self, -1, 
            wx.GetTranslation('Connecting to Google Calendar...')), 
            0, wx.ALIGN_CENTER, 10);
        sizerCountdown.Add((10, 0), 1, wx.EXPAND, 5)
        sizerCountdown.Add(self.staticCountdown, 0, wx.ALIGN_CENTER, 10)
    
        self.sizerProcess.Add(sizerCountdown, 0, wx.ALIGN_CENTER, 10)
        self.sizerProcess.Add((0, 5), 1, wx.EXPAND, 5)
        self.sizerProcess.Add(wx.Button(self, idConnectImmediately,
            wx.GetTranslation('Connect immediately'), wx.DefaultPosition, (130, 24)),
            0, wx.ALIGN_CENTER, 4)
        self.sizerProcess.Add((0, 2), 1, wx.EXPAND, 5)
        self.sizerProcess.Add(wx.Button(self, idStopConnecting,
            wx.GetTranslation('Stop connecting'), wx.DefaultPosition, (130, 24)),
            0, wx.ALIGN_CENTER, 4)
        
        self.sizerOpt.Add(self.cbRemAccount, 0, wx.ALL, 5)
        self.sizerOpt.Add(self.cbRemPasswd, 0, wx.ALL, 5)
        self.sizerOpt.Add(self.cbAutoLogin, 0, wx.ALL, 5)


        sizerOther = wx.BoxSizer(wx.HORIZONTAL)
        sizerOther.Add((0, 0), 1, wx.EXPAND, 5)
        sizerOther.Add(wx.HyperlinkCtrl(self, -1, 
            wx.GetTranslation('Visit Google'), 'http://www.google.com'), 0,
            wx.ALL, 7)
        sizerOther.Add(wx.HyperlinkCtrl(self, -1, 
            wx.GetTranslation('Visit Google Calendar'), 
            'http://www.google.com/calendar'), 0,
            wx.ALL, 7)

        self.sizer.Add(wx.StaticBitmap(self, -1, imagesplash.getSplashBitmap(), 
            wx.Point(0, 0), (400, 90))) 
        self.sizer.Add((0, 15), 1, wx.EXPAND, 5)
        self.sizer.Add(self.sizerMain, 1, wx.ALIGN_CENTER, 5)
        self.sizer.Add(self.sizerProcess, 1, wx.ALIGN_CENTER, 5)
        self.sizer.Add(self.sizerOpt, 1, wx.ALIGN_CENTER, 5)
        self.sizer.Add(sizerOther, 1, wx.EXPAND, 5)
        self.sizer.Show(self.sizerProcess, False)
            
    
        self.SetSizer(self.sizer)
        self.Layout();
        
        wx.EVT_CLOSE(self, self.OnClose)
        wx.EVT_BUTTON(self, idLoginButton, self.OnLogin)
        wx.EVT_INIT_DIALOG(self, self.OnCreate)
        wx.EVT_TIMER(self, idTimer, self.OnTimer)
        wx.EVT_BUTTON(self, idConnectImmediately, self.OnConnectImmediately)
        wx.EVT_BUTTON(self, idStopConnecting, self.OnStopConnecting)
        
        wx.EVT_ENTER_WINDOW(self.editAccount, self.OnEnterWindow)
        wx.EVT_ENTER_WINDOW(self.editPasswd, self.OnEnterWindow)
        
        self.editPasswd.Bind(wx.EVT_TEXT, self.OnPasswdChanged)
        
        self.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.editAccount.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        self.editPasswd.Bind(wx.EVT_KEY_DOWN, self.OnKeyDown)
        
        self.timer = wx.Timer(self, idTimer)
        
    def OnClose(self, event):
        self.Destroy()
    
    def OnKeyDown(self, event):
        if event.GetKeyCode == wx.WXK_ESCAPE:
            self.Close()
        elif event.GetKeyCode == wx.WXK_RETURN:
            wx.PostEvent(self, wx.CommandEvent(wx.EVT_BUTTON, idLoginButton))
        else:
            event.Skip()
    
    def OnLogin(self, event):
        self.Login()
        
    def OnTimer(self, event):
        seconds = int(self.staticCountdown.GetLabel())
        if seconds > 0:
            seconds -= 1
            self.staticCountdown.SetLabel(str(seconds))
        else:
            self.timer.Stop()
            self.Login()
        
    def OnConnectImmediately(self, event):
        self.Login()
        
    def OnStopConnecting(self, event):
        self.StopAutoLogin()

    def OnCreate(self, event):
        pass

    def OnEnterWindow(self, event):
        event.GetEventObject().SetFocus()
        event.GetEventObject().SetSelection(-1, -1)
        
    def Login(self):        
        email = self.editAccount.GetValue()
        if not self.bEncodedPasswd:
            passwd = self.editPasswd.GetValue()
        else:
            passwd = self.sPasswd
        self.formMain.bRemAccount = self.cbRemAccount.GetValue()
        self.formMain.bRemPasswd = self.cbRemPasswd.GetValue()
        self.formMain.bAutoLogin = self.cbAutoLogin.GetValue()

        self.GoogleCalendar.email = email
        self.GoogleCalendar.password = passwd
        try:
            wx.BeginBusyCursor()
            self.GoogleCalendar.ProgrammaticLogin()
            wx.EndBusyCursor()
            self.Close()
        except:
            wx.MessageBox(wx.GetTranslation('Login Failed!'))
            self.StopAutoLogin()
    
    def SetCheckboxes(self, ra, rp, al):
        self.cbRemAccount.SetValue(ra)
        self.cbRemPasswd.SetValue(rp)
        self.cbAutoLogin.SetValue(al)
        
    def SetAccount(self, a):
        self.editAccount.SetValue(a)
        
    def SetPasswd(self, p):
        self.editPasswd.SetValue(p)
        self.bEncodedPasswd = True

    def OnPasswdChanged(self, event):
        if not self.bEncodedPasswd:
            self.bEncodedPasswd = False

    def AutoLogin(self):
        self.staticCountdown.SetLabel('10')
        self.sizer.Show(self.sizerMain, False)
        self.sizer.Show(self.sizerOpt, False)
        self.sizer.Show(self.sizerProcess, True)
        self.Layout()
        self.timer.Start(1000)
    
    def StopAutoLogin(self):
        self.staticCountdown.SetLabel('0')
        self.sizer.Show(self.sizerMain, True)
        self.sizer.Show(self.sizerOpt, True)
        self.sizer.Show(self.sizerProcess, False)
        self.Layout()
        self.timer.Stop()
